package com.haipumi.biz.impl;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.haipumi.beans.haipumi_skin;
import com.haipumi.beans.haipumi_user;
import com.haipumi.biz.IUserBiz;
import com.haipumi.dao.IUserInfoDao;
import com.haipumi.dao.impl.UserInfoDaoImpl;
import com.haipumi.utils.SendMail;

public class UserBizImpl implements IUserBiz {

	// 多态构建db
	IUserInfoDao userdaoimpl = new UserInfoDaoImpl();

	@Override
	/**
	 * bizimpl中的login的方法，
	 */
	public haipumi_user login(haipumi_user user) {
		haipumi_user backuser = null;
		backuser = userdaoimpl.login(user);

		return backuser;
	}

	@Override
	/**
	 * 注册用户的bizimpl方法
	 */
	public haipumi_user adduser(haipumi_user user,String localhref) {
		boolean flag = false;

		//flag = userdaoimpl.addUser(user);
		haipumi_user backuser = null;
		String safecode = "暂无验证码";//
		
		boolean mailflag=sendMail(user,localhref);
		if(mailflag){
			flag=userdaoimpl.addUser(user);
			if(flag){
				backuser = userdaoimpl.findUserInfo(user.getUlogon());
			}
		}else{
			backuser=null;
		}
		
		return backuser;
	}
	
	public boolean sendMail(haipumi_user user,String localhref){
		// 发送邮件
		SendMail sm = new SendMail("smtp.163.com");
		String smtp = "smtp.163.com";
		String from = "18373368832@163.com";
		String to = user.getUlogon();
		String subject = "haipumi嗨扑米欢迎您";
		
//		localhref="127.0.0.1/haipumiBate2.0/";
		localhref=localhref.substring(0, 25);
		
		String content = "<center style=\"border:blue 1px solid;background:#999; color:#fff;\">尊敬的"+user.getUlogon()+"<br/>您的嗨扑米账号已经注册成功。<br/>嗨扑米，帮助他人，手留余香，请谨记您的账号信息：<br/>登录名："+user.getUlogon()+"<br/>昵称:"+user.getUlogon()+
				"<br/>密码：<font color=\"#999\">"+user.getUpwd()+"</font><br/><font color=\"#fff\">（请单击以选中密码查看，请谨记您的密码）</font><br/>以上为用户基本信息，请点击下面的链接激活嗨扑米账号，以正常使用。<br/>"+
				"<font>"+localhref+"cust.action?op=activateuser&ulogon="+user.getUlogon()+"</font>"+
				"<br/>用户信息不满意？请到用户个人管理页面进行更改<br/><font color=\"red\">邮件为系统发送，仅供激活用户账号，请勿回复，谢谢合作。</font> </center>";
		//"<a href=\""+localhref+"cust.action?op=activateuser&ulogon="+user.getUlogon()+
		System.out.println(content);
		String username = "18373368832@163.com";
		String password = "srjmkumterdkyiwz";
		boolean mailflag=sm.send(smtp, from, to, subject, content, username, password);
		return mailflag;
	}
	
	@Override
	public haipumi_user findUser(String ulogon) {
		haipumi_user user = null;
		user = userdaoimpl.findUserInfo(ulogon);
		return user;
	}
	
	public boolean activateUser(String ulogon){
		boolean flag=userdaoimpl.activateUser(ulogon);
		return flag;
	}

	@Override
	public haipumi_user findUnActivate(String ulogon) {
		haipumi_user user=userdaoimpl.findUnActivate(ulogon);
		return user;
	}

	@Override
	public haipumi_user newlogin(haipumi_user user) {
		haipumi_user backuser=userdaoimpl.newlogin(user);
		return backuser;
	}

	@Override
	public boolean savebaseinfo(haipumi_user user) {
		boolean flag=false;
		flag=userdaoimpl.savebaseinfo(user);
		return flag;
	}

	@Override
	public haipumi_skin changebackground(String uid, String sid) {
		boolean flag=false;
		haipumi_skin skin=null;
		flag=userdaoimpl.changebackground(uid,sid);
		if(flag){
			skin=userdaoimpl.findskin(sid);
		}
		return skin;
	}

	@Override
	public boolean saveTel(String uid, String uqq, String utel) {
		boolean flag=userdaoimpl.saveTel(uid, uqq, utel);
		return flag;
	}

}
